package com.paohe.lianxin_pda.production_line.chengpPei;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.paohe.lianxin_pda.R;
import com.paohe.lianxin_pda.utils.spinner.SpinnerUtils;

import java.util.List;

public class RecycleSonAdapter extends RecyclerView.Adapter<RecycleSonAdapter.MyViewHolder> {
    private Context context;
    private List<String[]> list;
    private RecyclerView rvCategory;

    public RecycleSonAdapter(Context context, List<String[]> list, RecyclerView rvCategory) {
        this.context = context;
        this.list = list;
        this.rvCategory = rvCategory;
    }

    @NonNull
    @Override
    public RecycleSonAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
                context).inflate(R.layout.fragment_son_item, parent,
                false));
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final RecycleSonAdapter.MyViewHolder holder, final int position) {

        //设置每一项的子控件是否可以编辑
        if(list.get(position)[6] == "1"){
            Drawable drawable = context.getDrawable(R.mipmap.down_arrow);
            drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
            holder.et_th.setCompoundDrawables(null,null,drawable,null);
            holder.et_th.setClickable(true);
            holder.et_th.setBackground(context.getResources().getDrawable(R.drawable.edit_bg));

            //只有选择了临时桶才可编辑
            if("临时桶".equals(list.get(position)[0]) || "".equals(list.get(position)[0])){
                holder.et_mz.setFocusable(true);
                holder.et_mz.setFocusableInTouchMode(true);
                holder.et_mz.setBackground(context.getResources().getDrawable(R.drawable.edit_bg));

                holder.et_tz.setFocusable(true);
                holder.et_tz.setFocusableInTouchMode(true);
                holder.et_tz.setBackground(context.getResources().getDrawable(R.drawable.edit_bg));

                holder.et_jz.setFocusable(true);
                holder.et_jz.setFocusableInTouchMode(true);
                holder.et_jz.setBackground(context.getResources().getDrawable(R.drawable.edit_bg));
            }else{
                //选择了固定规格桶，则无法编辑重量
                holder.et_mz.setFocusable(false);
                holder.et_mz.setFocusableInTouchMode(false);
                holder.et_mz.setBackground(null);

                holder.et_tz.setFocusable(false);
                holder.et_tz.setFocusableInTouchMode(false);
                holder.et_tz.setBackground(null);

                holder.et_jz.setFocusable(false);
                holder.et_jz.setFocusableInTouchMode(false);
                holder.et_jz.setBackground(null);
            }


        }else{
            holder.et_th.setClickable(false);
            holder.et_th.setCompoundDrawables(null,null,null,null);
            holder.et_th.setBackground(null);

            holder.et_mz.setFocusable(false);
            holder.et_mz.setFocusableInTouchMode(false);
            holder.et_mz.setBackground(null);

            holder.et_tz.setFocusable(false);
            holder.et_tz.setFocusableInTouchMode(false);
            holder.et_tz.setBackground(null);

            holder.et_jz.setFocusable(false);
            holder.et_jz.setFocusableInTouchMode(false);
            holder.et_jz.setBackground(null);
        }

        final String[] addObj = new String[]{"","","","","提交","#2C89E9","1"};
        holder.tv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (list.get(position)[4].equals("提交")) {
                    //点击提交
                    //先进行非空判断
                    if("".equals(holder.et_mz.getText().toString().trim())){
                        Toast.makeText(context,"毛重不能为空!",Toast.LENGTH_SHORT).show();;
                        return;
                    }
                    if("".equals(holder.et_tz.getText().toString().trim())){
                        Toast.makeText(context,"桶重不能为空!",Toast.LENGTH_SHORT).show();;
                        return;
                    }
                    if("".equals(holder.et_jz.getText().toString().trim())){
                        Toast.makeText(context,"净重不能为空!",Toast.LENGTH_SHORT).show();;
                        return;
                    }
                    //获取数据
                    addObj[0] = holder.et_th.getText().toString();
                    addObj[1] = holder.et_mz.getText().toString();
                    addObj[2] = holder.et_tz.getText().toString();
                    addObj[3] = holder.et_jz.getText().toString();
                    addData(position,addObj);

                }else {
                    //删除自带默认动画
                    removeData(position);
                }

            }

        });

        //获取下拉列表1的数据源
        final String[] mItems = context.getResources().getStringArray(R.array.sprinnerarr2);

        //下拉框点击事件
        holder.et_th.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SpinnerUtils mySpinner = new SpinnerUtils(context, holder.et_th, mItems, new SpinnerUtils.MyItemClickListener() {
                    @Override
                    public void onClick(View view) {

                        //根据下拉的选择，填充数据
                        if(mItems[(int)view.getTag()].equals("001号桶")){
                            //赋值
                            addObj[1]= "10";
                            addObj[2]= "10";
                            addObj[3]= "10";
                            list.get(position)[0] = mItems[(int)view.getTag()];
                            list.get(position)[1] = addObj[1];
                            list.get(position)[2] = addObj[2];
                            list.get(position)[3] = addObj[3];

                        }else if(mItems[(int)view.getTag()].equals("002号桶")){
                            //赋值
                            addObj[1]= "20";
                            addObj[2]= "20";
                            addObj[3]= "20";
                            list.get(position)[0] = mItems[(int)view.getTag()];
                            list.get(position)[1] = addObj[1];
                            list.get(position)[2] = addObj[2];
                            list.get(position)[3] = addObj[3];
                        }else if(mItems[(int)view.getTag()].equals("003号桶")){
                            //赋值
                            addObj[1]= "30";
                            addObj[2]= "30";
                            addObj[3]= "30";
                            list.get(position)[0] = mItems[(int)view.getTag()];
                            list.get(position)[1] = addObj[1];
                            list.get(position)[2] = addObj[2];
                            list.get(position)[3] = addObj[3];
                        }else{
                            //赋值
                            addObj[1]= "";
                            addObj[2]= "";
                            addObj[3]= "";
                            list.get(position)[0] = mItems[(int)view.getTag()];
                            list.get(position)[1] = addObj[1];
                            list.get(position)[2] = addObj[2];
                            list.get(position)[3] = addObj[3];
                        }

                        //通知适配器进行更新
                        notifyDataSetChanged();
                    }
                });
                mySpinner.showPopupWindow();

            }
        });


        if(list.get(position)[0] == "" || list.get(position)[0] == null){
            holder.et_th.setText("临时桶");
        }else{
            holder.et_th.setText(list.get(position)[0]);
        }

        //设置编辑框的值
        holder.et_mz.setText(list.get(position)[1]);
        holder.et_tz.setText(list.get(position)[2]);
        holder.et_jz.setText(list.get(position)[3]);

        //设置操作按钮的颜色以及文字
        holder.tv_delete.setText(list.get(position)[4]);
        holder.tv_delete.setTextColor(Color.parseColor(list.get(position)[5]));

    }

    @Override
    public int getItemCount() {
        return list.size();
    }
    //  添加数据
    public void addData(int position,String[] addObj) {

        //设置上一行的值
        list.get(position)[0] = addObj[0];
        list.get(position)[1] = addObj[1];
        list.get(position)[2] = addObj[2];
        list.get(position)[3] = addObj[3];
        list.get(position)[4] = "删除";
        list.get(position)[5] = "#770124";
        list.get(position)[6] = "0";//设置不可编辑

        //设置新增的数据为空
        addObj[0] = "";
        addObj[1] = "";
        addObj[2] = "";
        addObj[3] = "";
        addObj[4] = "提交";
        addObj[5] = "#2C89E9";
        addObj[6] = "1";//设置可编辑
//      在list中添加数据，并通知条目加入一条
        list.add(position+1,addObj);

        //添加动画
        notifyItemInserted(position+1);

        //局部更新适配器
        notifyItemRangeChanged(position,getItemCount());

    }
    //  删除数据
    public void removeData(int position) {
        list.remove(position);

        //删除动画
        notifyItemRemoved(position);

        //更新适配器
        notifyDataSetChanged();

        //局部更新适配器
//        if(position != list.size()){
//            notifyItemRangeChanged(position,list.size()-position);
//        }

    }

    /**
     * ViewHolder的类，用于缓存控件
     */
    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv_delete,et_th;
        EditText et_mz,et_tz,et_jz;
        //因为删除有可能会删除中间条目，然后会造成角标越界，所以必须整体刷新一下！
        public MyViewHolder(View view) {
            super(view);
//            et_th = view.findViewById(R.id.son_et_th);

            et_th = view.findViewById(R.id.son_et_th);
            et_mz = view.findViewById(R.id.son_et_mz);
            et_tz = view.findViewById(R.id.son_et_tz);
            et_jz = view.findViewById(R.id.son_et_jz);
            tv_delete = view.findViewById(R.id.son_tv_delete);

        }
    }
}
